<!DOCTYPE html>
<html lang="en"><head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.8.0 -->
<title>0034: Menus IX - AccelGroup as a Singleton | gtkDcoding</title>
<meta name="generator" content="Jekyll v4.3.4" />
<meta property="og:title" content="0034: Menus IX - AccelGroup as a Singleton" />
<meta name="author" content="Ron Tarrant" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Wrapping a GTK AccelGroup in a Singleton using the D language." />
<meta property="og:description" content="Wrapping a GTK AccelGroup in a Singleton using the D language." />
<link rel="canonical" href="http://localhost:4000/posts/0034-accelgroup-singleton.html" />
<meta property="og:url" content="http://localhost:4000/posts/0034-accelgroup-singleton.html" />
<meta property="og:site_name" content="gtkDcoding" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2019-05-10T00:00:00-04:00" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="0034: Menus IX - AccelGroup as a Singleton" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"Ron Tarrant"},"dateModified":"2019-05-10T00:00:00-04:00","datePublished":"2019-05-10T00:00:00-04:00","description":"Wrapping a GTK AccelGroup in a Singleton using the D language.","headline":"0034: Menus IX - AccelGroup as a Singleton","mainEntityOfPage":{"@type":"WebPage","@id":"http://localhost:4000/posts/0034-accelgroup-singleton.html"},"url":"http://localhost:4000/posts/0034-accelgroup-singleton.html"}</script>
<!-- End Jekyll SEO tag -->
<link rel="stylesheet" href="../assets/main.css">
  <link rel="stylesheet" href="../css/style.css" /><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="gtkDcoding" /></head>
<body><header class="site-header" role="banner">

  <div class="wrapper"><a class="site-title" rel="author" href="../">gtkDcoding</a><nav class="site-nav">
        <input type="checkbox" id="nav-trigger" class="nav-trigger" />
        <label for="nav-trigger">
          <span class="menu-icon">
            <svg viewBox="0 0 18 15" width="18px" height="15px">
              <path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"/>
            </svg>
          </span>
        </label>

        <div class="trigger"><a class="page-link" href="../about.html">About</a><a class="page-link" href="../index-reverse.html">Posts in Date Order</a><a class="page-link" href="../topics.html">Blog Posts by Topic</a></div>
      </nav></div>
</header>
<main class="page-content" aria-label="Content">
      <div class="wrapper">
        <article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">
	<header class="post-header">
		<link rel="stylesheet" href="../css/font_size.css" />
		<link rel="stylesheet" href="../css/figure.css" />
		<link rel="stylesheet" href="../css/topics/menu.css" />  <!-- sub in the tag/topic -->
		<link rel="stylesheet" href="../css/modal.css" />
		<link rel="stylesheet" href="../css/post.css" />
		<link rel="stylesheet" href="../css/mascot_effects.css" />

		
		
		<p class="post-meta">
		<time class="dt-published" datetime="2019-05-10T00:00:00-04:00" itemprop="datePublished">Friday, May 10, 2019
		</time>• by <span itemprop="author" itemscope itemtype="http://schema.org/Person"><span class="p-author h-card" itemprop="name">Ron Tarrant</span></span></p>
	</header>

	<div class="post-content e-content" itemprop="articleBody">
		<div class="skew-line"></div>
		<h1 id="0034-menus-ix---accelgroup-as-a-singleton">0034: Menus IX - AccelGroup as a Singleton</h1>

<div class="screenshot-frame">
	<div class="frame-header">
		Results of this example:
	</div>
	<div class="frame-screenshot">
		<figure>
			<img id="img0" src="../images/screenshots/012_menus/menu_18.png" alt="Current example output" />		<!-- img# -->
			
			<!-- Modal for screenshot -->
			<div id="modal0" class="modal">																	<!-- modal# -->
				<span class="close0">&times;</span>															<!-- close# -->
				<img class="modal-content" id="img00" />															<!-- img## -->
				<div id="caption"></div>
			</div>
			
			<script>
			// Get the modal
			var modal = document.getElementById("modal0");														// modal#
			
			// Get the image and insert it inside the modal - use its "alt" text as a caption
			var img = document.getElementById("img0");															// img#
			var modalImg = document.getElementById("img00");													// img##
			var captionText = document.getElementById("caption");

			img.onclick = function()
			{
			  modal.style.display = "block";
			  modalImg.src = this.src;
			  captionText.innerHTML = this.alt;
			}
			
			// Get the <span> element that closes the modal
			var span = document.getElementsByClassName("close0")[0];											// close#
			
			// When the user clicks on <span> (x), close the modal
			span.onclick = function()
			{ 
				modal.style.display = "none";
			}
			</script>
			<figcaption>
			Current example output
			</figcaption>
		</figure>
	</div>

	<div class="frame-terminal">
		<figure class="right">
			<img id="img1" src="../images/screenshots/012_menus/menu_18_term.png" alt="Current example terminal output" />		<!-- img#, filename -->

			<!-- Modal for terminal shot -->
			<div id="modal1" class="modal">																				<!-- modal# -->
				<span class="close1">&times;</span>																		<!-- close# -->
				<img class="modal-content" id="img11" />																		<!-- img## -->
				<div id="caption"></div>
			</div>
			
			<script>
			// Get the modal
			var modal = document.getElementById("modal1");																	// modal#
			
			// Get the image and insert it inside the modal - use its "alt" text as a caption
			var img = document.getElementById("img1");																		// img#
			var modalImg = document.getElementById("img11");																// img##
			var captionText = document.getElementById("caption");

			img.onclick = function()
			{
			  modal.style.display = "block";
			  modalImg.src = this.src;
			  captionText.innerHTML = this.alt;
			}
			
			// Get the <span> element that closes the modal
			var span = document.getElementsByClassName("close1")[0];														// close#
			
			// When the user clicks on <span> (x), close the modal
			span.onclick = function()
			{ 
				modal.style.display = "none";
			}
			</script>

			<figcaption>
				Current example terminal output (click for enlarged view)
			</figcaption>
		</figure>
	</div>

	<div class="frame-footer">																								<!-- ------------- filename (below) --------- -->
		The code file for this example is available <a href="https://github.com/rontarrant/gtkd_demos/blob/master/012_menus/menu_18_singleton_accel_menus.d" target="_blank">here</a>.
	</div>
</div>

<p>Something about how the <code class="language-plaintext highlighter-rouge">AccelGroup</code> was used in <em>gtkDcoding</em> blog post #32 (<a href="./0032-accelerator-keys.html"><em>Adding Accelerator Keys to MenuItems</em></a>) needs to be addressed. It’s this business of instantiating the <code class="language-plaintext highlighter-rouge">AccelGroup</code> at the top level (ie. <code class="language-plaintext highlighter-rouge">TestRigWindow</code>) and then passing it down through multiple layers of other UI objects until it gets to where it’s actually needed, in the <code class="language-plaintext highlighter-rouge">MenuItem</code> objects.</p>

<p>Not that there’s anything technically wrong with passing the AccelGroup object down like that. It works, so why fix it?</p>

<p>Because if we use a singleton class to hold the <code class="language-plaintext highlighter-rouge">AccelGroup</code>, we gain a few things:</p>

<ul>
  <li>we only need to deal with the <code class="language-plaintext highlighter-rouge">AccelGroup</code> where it’s actually needed:
    <ul>
      <li>in the top-level window, and</li>
      <li>in the <code class="language-plaintext highlighter-rouge">MenuItem</code> class definitions that have shortcut keys,</li>
    </ul>
  </li>
  <li>which means we can dispense with the cumbersome-ness of handing down, and</li>
  <li>we gain elegance.</li>
</ul>

<p>And for good measure, we’ll cover some <em>Other Useful Stuff <sup>TM</sup></em> as we go along.</p>

<h2 id="the-singleton">The Singleton</h2>

<p>There’s a lot of discussion about whether or not the singleton should be taken out back and shot. All I will say is that, because it can <em>only</em> be instantiated once, no matter how many times we try, we avoid the ad infinitum passing down and this makes it an elegant solution for the AccelGroup.</p>

<p>Be aware, however, that we don’t instantiate in the normal way. But let’s not get ahead of ourselves. We’ll start with just the naked singleton and go from there.</p>

<p>What we’re starting with to build our <code class="language-plaintext highlighter-rouge">S_AccelGroup</code> class can be found in <a href="https://wiki.dlang.org/Low-Lock_Singleton_Pattern">the D Wiki Low-lock Singleton example</a>. And here’s what the basic class looks like:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="n">MySingleton</span>
<span class="p">{</span>
    <span class="k">private</span> <span class="k">this</span><span class="p">()</span> <span class="p">{}</span>

    <span class="c1">// Cache instantiation flag in thread-local bool</span>
    <span class="k">private</span> <span class="k">static</span> <span class="kt">bool</span> <span class="n">instantiated_</span><span class="p">;</span>

    <span class="c1">// Thread global</span>
    <span class="k">private</span> <span class="k">__gshared</span> <span class="n">MySingleton</span> <span class="n">instance_</span><span class="p">;</span>

    <span class="k">static</span> <span class="n">MySingleton</span> <span class="n">get</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">if</span><span class="p">(!</span><span class="n">instantiated_</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">synchronized</span><span class="p">(</span><span class="n">MySingleton</span><span class="p">.</span><span class="n">classinfo</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">if</span><span class="p">(!</span><span class="n">instance_</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">instance_</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MySingleton</span><span class="p">();</span>
                <span class="p">}</span>

                <span class="n">instantiated_</span> <span class="p">=</span> <span class="kc">true</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="k">return</span><span class="p">(</span><span class="n">instance_</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Seems complicated, right? Well, the only things we really need to know are:</p>

<ul>
  <li>we instantiate or get a reference with a call to <code class="language-plaintext highlighter-rouge">get()</code> wherever we need to deal with this object,</li>
  <li>never <em>ever</em> call the constructor (if we do this up right, we can’t call it directly, anyway),</li>
  <li>the <code class="language-plaintext highlighter-rouge">private</code> keyword will only do its job if this class is in a <code class="language-plaintext highlighter-rouge">module</code> of its own, and</li>
  <li>anything we add to this to make it useful goes into the constructor.</li>
</ul>

<p>So, after a few naming convention changes, our <code class="language-plaintext highlighter-rouge">S_AccelGroup</code> class (which you can find in <a href="https://github.com/rontarrant/gtkd_demos/blob/master/012_menus/singleton/S_AccelGroup.d">this example file right here</a>) looks like this:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">module</span> <span class="n">singleton</span><span class="p">.</span><span class="n">S_AccelGroup</span><span class="p">;</span>

<span class="k">import</span> <span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">;</span>

<span class="k">import</span> <span class="n">gtk</span><span class="p">.</span><span class="n">AccelGroup</span><span class="p">;</span>

<span class="k">class</span> <span class="n">S_AccelGroup</span> <span class="p">:</span> <span class="n">AccelGroup</span>
<span class="p">{</span>
	<span class="k">private</span><span class="p">:</span>
	<span class="c1">// Cache instantiation flag in thread-local bool</span>
	<span class="k">static</span> <span class="kt">bool</span> <span class="n">instantiated_</span><span class="p">;</span>

	<span class="c1">// Thread global</span>
	<span class="k">__gshared</span> <span class="n">S_AccelGroup</span> <span class="n">instance_</span><span class="p">;</span>

	<span class="k">this</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="k">super</span><span class="p">();</span>

	<span class="p">}</span> <span class="c1">// this()</span>

	<span class="k">public</span><span class="p">:</span>
	
	<span class="k">static</span> <span class="n">S_AccelGroup</span> <span class="n">get</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="n">write</span><span class="p">(</span><span class="s">"getting..."</span><span class="p">);</span>
		
		<span class="k">if</span><span class="p">(!</span><span class="n">instantiated_</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="k">synchronized</span><span class="p">(</span><span class="n">S_AccelGroup</span><span class="p">.</span><span class="n">classinfo</span><span class="p">)</span>
			<span class="p">{</span>
				<span class="k">if</span><span class="p">(!</span><span class="n">instance_</span><span class="p">)</span>
				<span class="p">{</span>
					<span class="n">instance_</span> <span class="p">=</span> <span class="k">new</span> <span class="n">S_AccelGroup</span><span class="p">();</span>
					<span class="n">writeln</span><span class="p">(</span><span class="s">"creating"</span><span class="p">);</span>
				<span class="p">}</span>

				<span class="n">instantiated_</span> <span class="p">=</span> <span class="kc">true</span><span class="p">;</span>
			<span class="p">}</span>
		<span class="p">}</span>
		<span class="k">else</span>
		<span class="p">{</span>
			<span class="n">writeln</span><span class="p">(</span><span class="s">"not created"</span><span class="p">);</span>
		<span class="p">}</span>

		<span class="k">return</span><span class="p">(</span><span class="n">instance_</span><span class="p">);</span>
		
	<span class="p">}</span> <span class="c1">// get()</span>

<span class="p">}</span> <span class="c1">// class S_AccelGroup</span>
</code></pre></div></div>

<p><em>Note: If you use this locally, don’t forget to put this in its own sub-directory.</em></p>

<p>Right at the top, we have:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">module</span> <span class="n">S_AccelGroup</span><span class="p">;</span>
</code></pre></div></div>

<p>Technically, you could put a similar statement at the top of every one of your <code class="language-plaintext highlighter-rouge">.d</code> files, turning them all into modules and it wouldn’t really change anything for that code file, per se. What it does is give us a way to import it for those times when we want the code from one file available to another.</p>

<p><em>Note that the <code class="language-plaintext highlighter-rouge">module</code> statement has to be the first statement in the file. You can have a comment above it, but nothing else.</em></p>

<p>The file needs to be saved as <code class="language-plaintext highlighter-rouge">S_AccelGroup.d</code> (the <code class="language-plaintext highlighter-rouge">module</code> name plus a <code class="language-plaintext highlighter-rouge">.d</code> extension) and just for kicks, I put it in its own sub-directory which means the path and filename in relation to our dev directory is:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">./</span><span class="n">singleton</span><span class="p">/</span><span class="n">S_AccelGroup</span><span class="p">.</span><span class="n">d</span>
</code></pre></div></div>

<p>Hold that thought as we go into the next section…</p>

<h2 id="other-useful-stuff-1-using-an-external-class">Other Useful Stuff #1: Using an External Class</h2>

<p>To bring the external file into the mix for compiling/linking, we need an <code class="language-plaintext highlighter-rouge">import</code> statement which you’ll find near the top of today’s primary code file. Any time the file you want to import is in a sub-directory, replace the slash (<code class="language-plaintext highlighter-rouge">/</code>) with a dot (<code class="language-plaintext highlighter-rouge">.</code>) in your import statement… like this:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="n">singleton</span><span class="p">.</span><span class="n">S_AccelGroup</span><span class="p">;</span>
</code></pre></div></div>

<p>If I’d put this file in the same directory as our primary <code class="language-plaintext highlighter-rouge">.d</code> file, the import statement would look like this instead:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="n">S_AccelGroup</span><span class="p">;</span>
</code></pre></div></div>

<p>Okay, now you can let go of that thought from the previous section.</p>

<h2 id="attaching-the-s_accelgroup-to-the-mainwindow">Attaching the S_AccelGroup to the MainWindow</h2>

<p>Just like with the stock <code class="language-plaintext highlighter-rouge">AccelGroup</code>, we need to attach it to the <code class="language-plaintext highlighter-rouge">MainWindow</code> which means our <code class="language-plaintext highlighter-rouge">TestRigWindow</code> class now looks like this:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="n">TestRigWindow</span> <span class="p">:</span> <span class="n">MainWindow</span>
<span class="p">{</span>
	<span class="nb">string</span> <span class="n">title</span> <span class="p">=</span> <span class="s">"Multiple Menus Example"</span><span class="p">;</span>
	<span class="n">S_AccelGroup</span> <span class="n">s_AccelGroup</span><span class="p">;</span>

	<span class="k">this</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="k">super</span><span class="p">(</span><span class="n">title</span><span class="p">);</span>
		<span class="n">setDefaultSize</span><span class="p">(</span><span class="mi">640</span><span class="p">,</span> <span class="mi">480</span><span class="p">);</span>
		<span class="n">addOnDestroy</span><span class="p">(&amp;</span><span class="n">quitApp</span><span class="p">);</span>

		<span class="n">s_AccelGroup</span> <span class="p">=</span> <span class="n">s_AccelGroup</span><span class="p">.</span><span class="n">get</span><span class="p">();</span>
		<span class="n">addAccelGroup</span><span class="p">(</span><span class="n">s_AccelGroup</span><span class="p">);</span>

		<span class="n">AppBox</span> <span class="n">appBox</span> <span class="p">=</span> <span class="k">new</span> <span class="n">AppBox</span><span class="p">();</span>
		<span class="n">add</span><span class="p">(</span><span class="n">appBox</span><span class="p">);</span>
		
		<span class="n">showAll</span><span class="p">();</span>
		
	<span class="p">}</span> <span class="c1">// this()</span>
	
	
	<span class="kt">void</span> <span class="n">quitApp</span><span class="p">(</span><span class="n">Widget</span> <span class="n">w</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="c1">// do other quit stuff here if necessary</span>
		
		<span class="n">Main</span><span class="p">.</span><span class="n">quit</span><span class="p">();</span>
		
	<span class="p">}</span> <span class="c1">// quitApp()</span>
	
<span class="p">}</span> <span class="c1">// class TestRigWindow</span>
</code></pre></div></div>

<p>I’ll draw your attention to three things here:</p>

<ul>
  <li>the definition of <code class="language-plaintext highlighter-rouge">s_AccelGroup</code> near the top,</li>
  <li>the constructor where we call <code class="language-plaintext highlighter-rouge">s_AccelGroup.get()</code> to instantiate, and</li>
  <li>also in the constructor, where we <code class="language-plaintext highlighter-rouge">addAccelGroup()</code> to attach it to the window.</li>
</ul>

<h2 id="the-other-end-of-things-inside-the-menuitem-class">The Other End of Things: Inside the MenuItem Class</h2>

<p>For each <code class="language-plaintext highlighter-rouge">MenuItem</code> that has a hotkey, we rewrite its definition to look similar to this:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="n">NewFileItem</span> <span class="p">:</span> <span class="n">MenuItem</span>
<span class="p">{</span>
	<span class="nb">string</span> <span class="n">itemLabel</span> <span class="p">=</span> <span class="s">"New"</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">accelKey</span> <span class="p">=</span> <span class="sc">'n'</span><span class="p">;</span>
	<span class="n">S_AccelGroup</span> <span class="n">s_AccelGroup</span><span class="p">;</span>
	   
	<span class="k">this</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="n">s_AccelGroup</span> <span class="p">=</span> <span class="n">s_AccelGroup</span><span class="p">.</span><span class="n">get</span><span class="p">();</span>
		<span class="k">super</span><span class="p">(&amp;</span><span class="n">doSomething</span><span class="p">,</span> <span class="n">itemLabel</span><span class="p">,</span> <span class="s">"activate"</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="n">s_AccelGroup</span><span class="p">,</span> <span class="n">accelKey</span><span class="p">,</span> <span class="n">ModifierType</span><span class="p">.</span><span class="n">CONTROL_MASK</span><span class="p">,</span> <span class="n">AccelFlags</span><span class="p">.</span><span class="n">VISIBLE</span><span class="p">);</span>
		
	<span class="p">}</span> <span class="c1">// this()</span>
	
	
	<span class="kt">void</span> <span class="n">doSomething</span><span class="p">(</span><span class="n">MenuItem</span> <span class="n">mi</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="n">writeln</span><span class="p">(</span><span class="s">"New file created."</span><span class="p">);</span>
		
	<span class="p">}</span> <span class="c1">// doSomething()</span>
	
<span class="p">}</span> <span class="c1">// class NewFileItem</span>
</code></pre></div></div>

<p>Again, we need three statements to set up and use the <code class="language-plaintext highlighter-rouge">S_AccelGroup</code>:</p>

<ul>
  <li>the declaration near the top,</li>
  <li>the call to <code class="language-plaintext highlighter-rouge">s_AccelGroup.get()</code> in the constructor, and</li>
  <li>the fifth argument that’s passed to <code class="language-plaintext highlighter-rouge">super()</code>.</li>
</ul>

<p>And if you want to prove to yourself that all these <code class="language-plaintext highlighter-rouge">MenuItem</code>s really do use the same <code class="language-plaintext highlighter-rouge">S_AccelGroup</code>, add this line as the last statement in the constructors of a few of the <code class="language-plaintext highlighter-rouge">MenuItem</code>s:</p>

<div class="language-d highlighter-rouge"><div class="highlight"><pre class="highlight"><code>	<span class="n">writeln</span><span class="p">(&amp;</span><span class="n">s_AccelGroup</span><span class="p">);</span>
</code></pre></div></div>

<p>The address echoed to the terminal will be the same for each.</p>

<h2 id="other-useful-stuff-2-important-compiling-note">Other Useful Stuff #2: Important Compiling Note</h2>

<p>When you go to compile this, because our code is now in two separate files, you’ll need to add the <code class="language-plaintext highlighter-rouge">–i</code> switch on the command line. Here’s the Windows version:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dmd –de –w –m64 –Lgtkd.lib –i &lt;filename&gt;.d
</code></pre></div></div>

<p>It’s the same in Linux, just add <code class="language-plaintext highlighter-rouge">–i</code> to whatever compile command you usually use:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dmd -de -w -m64  -i -I/usr/include/dmd/gtkd3 -L-L/usr/lib/x86_64-linux-gnu -L-L/usr/lib/i386-linux-gnu -L-l:libgtkd-3.so -L-l:libdl.so.2 -L--no-warn-search-mismatch -defaultlib=libphobos2.so &lt;code-filename&gt;.d -of&lt;executable-filename&gt;
</code></pre></div></div>

<p>or the short form:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dmd -de -w -m64  -i `pkg-config --cflags --libs gtkd-3` &lt;code-filename&gt;.d -of&lt;executable-filename&gt;
</code></pre></div></div>

<p>or the alias:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dbuild -i &lt;code_filename&gt;.d
</code></pre></div></div>

<h2 id="conclusion">Conclusion</h2>

<p>Even though doing the <code class="language-plaintext highlighter-rouge">AccelGroup</code> up as a singleton isn’t necessary, it’s more elegant and more readily reusable. We just need to remember that wherever we refer to the singleton, no matter what it’s used for, we:</p>

<ul>
  <li>define it,</li>
  <li>call <code class="language-plaintext highlighter-rouge">&lt;singleton_name&gt;.get()</code> to instantiate or get a reference, and</li>
  <li>then use whatever we’ve added to the singleton’s constructor for whatever nefarious purpose we have.</li>
</ul>

<p>As an extra bonus today, we also went over how to split our code over more than one file. In a nutshell:</p>

<ul>
  <li>move the code for a class into its own file,
    <ul>
      <li>name it using CamelCase, and</li>
      <li>give it a <code class="language-plaintext highlighter-rouge">.d</code> extension,</li>
    </ul>
  </li>
  <li>to the very top of the file, add the line:
    <ul>
      <li><code class="language-plaintext highlighter-rouge">module &lt;FileNameWithoutExtension&gt;;</code></li>
    </ul>
  </li>
  <li><code class="language-plaintext highlighter-rouge">import</code> it into your primary code file just as you would any other library or wrapper, and</li>
  <li>use the <code class="language-plaintext highlighter-rouge">–i</code> flag when compiling.</li>
</ul>

<p>And that’s it for another <em>gtkDcoding</em> blog post. Until next time…</p>

<div class="blog-nav">
	<div style="float: left;">
		<a href="./0033-fake-image-menu-and-accel.html">Previous: Fake ImageMenuItem</a>
	</div>
	<div style="float: right;">
		<a href="./0035-help-about-dialog.html">Next: Dialog I: About Dialog</a>
	</div>
</div>
<p>
	<h3></h3>
<div class="inpage-frame">
	<a href="https://github.com/sponsors/rontarrant">
		<BR>
		<BR>
		<B>Is this article useful? Educational? Entertaining, perhaps?</B>
		<BR>
		<figure class="left">
			<img src="../images/favorite_32.png" alt="Sponsorship heart" style="width: 32px; height: 32px;">
		</figure>
		You're free to accept or decline this invitation to become our newest sponsor.
	</a>
</div>
	<h4 class="comment-blurb"><B>Comments? Questions? Observations?</B></h4>
	<p>
		Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.
	</p>
	<script src="https://utteranc.es/client.js"
		repo="rontarrant"
		issue-term="pathname"
		theme="github-light"
		crossorigin="anonymous"
		async>
	</script>
	<ul>
		<li> come on over to the <a href="https://forum.dlang.org/">D Language Forum</a> and look for one of the <i>gtkDcoding</i> announcement posts, </li>
		<li> drop by <a href="https://forum.gtkd.org/">the <i>GtkD Forum</i></a>,</li>
		<li> follow the link below to email me, or</li>
		<li> go to the	<a href="https://www.facebook.com">gtkDcoding Facebook page.</a></li>
	</ul>
</p>
<p> You can also subscribe <a href="/feed.xml">via RSS</a> so you won't miss anything. Thank you very much for dropping by.</p>
<p>&copy; Copyright 2025 Ron Tarrant </p>
</div>

	<a class="u-url" href="./0034-accelgroup-singleton.html" hidden></a>
</article>

      </div>
    </main><footer class="site-footer h-card">
  <data class="u-url" href="/"></data>

  <div class="wrapper">

    <h2 class="footer-heading">gtkDcoding</h2>

    <div class="footer-col-wrapper">
      <div class="footer-col footer-col-1">
        <ul class="contact-list">
          <li class="p-name">Ron Tarrant</li><li><a class="u-email" href="mailto:gtkDcoding@gmail.com">gtkDcoding@gmail.com</a></li></ul>
      </div>

      <div class="footer-col footer-col-2"><ul class="social-media-list"><li><a href="https://github.com/rontarrant"><svg class="svg-icon"><use xlink:href="../assets/minima-social-icons.svg#github"></use></svg> <span class="username">rontarrant</span></a></li></ul>
</div>

      <div class="footer-col footer-col-3">
        <p>Simple examples of how to use GtkD to build GUI applications.</p>
      </div>
    </div>

  </div>

</footer>
</body>

</html>
